<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 注意:
    1.！！！！vue使用数据时，名称前面一定要加this.
    2.！！！JS使用条件相等判断时  注意 == 的使用，老是写成 =

    问题遗留：
    1 未实现 点击btn实现goodsinfo和goodslist同时变化---
    -->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>魔改商品清单练习</title>
    <style type="text/css">
        #app table *{
            padding: 10px;
            text-align: center;
        }
        #app table , tr, td, th{
            border: 1px solid black;
            border-collapse: collapse;
        }
    </style>
</head>
<body>
    <div id="app">

        <div id="goodsInfo">
            <b>GOODINFO</b>
            <br />
            <!-- ！！！v-for要求的为每个元素附唯一key值，此处先不管！！ -->
            <template v-for="item in goodsData">
                <button @click="goodsInfoCurrent=item.name">{{item.name}}</button>
                <!-- 双击事件加上去无效 -->
            </template>
            <table>
                <tr>
                    <th>商品编号</th>
                    <th>商品名称</th>
                    <th>商品单价</th>
                </tr>

                <tr v-if="goodsInfoCurrent==false">
                    <td colspan="3">点击显示信息，双击加入GOODLIST</td>
                </tr>
                <tr v-else v-for="item in filterGoodsInfo">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.price}}</td>
                </tr>
            </table>

        </div>
        <br />
        <hr />
        <table>
            <!-- nav -->
            <caption>
                <b>GOODSLIST</b>
            </caption>
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>商品单价</th>
                <th>商品数量</th>
                <th>商品总价</th>
                <th>商品操作</th>
            </tr>

            <tr v-if="goodsData.every(item => item.count==0)">
                <td colspan="6">购物车为空</td>
            </tr>

            <!-- info -->
            <tr v-for="item in filterGoodsData">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>
                    <button @click="item.count--">&minus;</button>
                    <span>{{item.count}}</span>
                    <button @click="item.count++">&plus;</button>
                </td>
                <td>
                    {{item.price*item.count}}
                </td>
                <td>
                    <button @click="item.count=0">删除</button>
                </td>
            </tr>

            <tr>
                <th colspan="4">总价</th>
                <td colspan="2">{{totalPrice}}</td>
            </tr>
        </table>
    </div>

    <script src="vue.js"></script>
    <script>
        new Vue({
            el:"#app",
            data:function(){
                return {
                    goodsData:[
                        {id:"001",name:"apple",price:10,count:1},
                        {id:"002",name:"banana",price:20,count:1},
                        {id:"003",name:"pear",price:30,count:1},
                        {id:"004",name:"xx04",price:30,count:0},
                        {id:"005",name:"xx05",price:10,count:0},
                        {id:"006",name:"xx06",price:50,count:0},
                        {id:"007",name:"xx07",price:30,count:0},
                    ],
                    goodsInfoCurrent:false,
                }
            },
            computed:{
                filterGoodsInfo:{
                    get:function(){
                        console.log("被调用");
                        return this.goodsData.filter(item => item.name==this.goodsInfoCurrent);
                    }
                    
                },

                filterGoodsData:function(){
                    return this.goodsData.filter(item => item.count>0);
                },

                totalPrice:function(){
                    return this.goodsData.reduce((sum,item)=>
                    {return sum+item.price*item.count},0)
                }
            },
            watch:{
                goodsInfoCurrent:function(){
                    console.log("hello")
                }
            },

            methods:{

            }
        })
    </script>
</body>
</html>